/*
 * *
 *  * Created with IntelliJ RustRover.
 *  * Description:
 *  * Copyright Author: ZhenYi
 *  * DateTime: 2024/2/14 下午5:10
 *
 */
use salvo::prelude::*;
use crate::utensil::cookie_local::LocalCookie;

/// 中间件
/// 检查是否是管理员
#[handler]
pub async fn check_admin(
    req: &mut Request,
    depot: &mut Depot,
    res: &mut Response,
    ctrl: &mut FlowCtrl,
) {
    let cookie = match req.cookie("Cookie") {
        None => {
            res.status_code(StatusCode::IM_USED);
            res.render("{\"Error\":\"Cookie not found\"}");
            ctrl.skip_rest();
            return;
        }
        Some(odk) => odk,
    };
    let local = depot.obtain::<LocalCookie>().unwrap();
    let value = local.is_admin(cookie.clone()).await;
    if !value  {
        res.status_code(StatusCode::IM_USED);
        res.render("{\"Error\":\"User not admin\"}");
        ctrl.skip_rest();
    } else {
        let mut nck = req.cookie("Cookie").unwrap().clone();
        nck.set_expires(LocalCookie::future_60min());
        nck.set_path("/");
        res.add_cookie(nck.clone());
    }
}
/// 中间件
/// 检查是否是作者
#[handler]
pub async fn check_author(
    req: &mut Request,
    depot: &mut Depot,
    res: &mut Response,
    ctrl: &mut FlowCtrl,
) {
    let cookie = match req.cookie("Cookie") {
        None => {
            res.status_code(StatusCode::IM_USED);
            res.render("{\"Error\":\"Cookie not found\"}");
            ctrl.skip_rest();
            return;
        }
        Some(odk) => odk,
    };
    let local = depot.obtain::<LocalCookie>().unwrap();
    let value = local.is_author(cookie.clone()).await;
    if !value  {
        res.status_code(StatusCode::IM_USED);
        res.render("{\"Error\":\"User not admin\"}");
        ctrl.skip_rest();
    } else {
        let mut nck = req.cookie("Cookie").unwrap().clone();
        nck.set_expires(LocalCookie::future_60min());
        nck.set_path("/");
        res.add_cookie(nck.clone());
    }
}
/// 中间件
/// 检查是否是专家
#[handler]
pub async fn check_expert(
    req: &mut Request,
    depot: &mut Depot,
    res: &mut Response,
    ctrl: &mut FlowCtrl,
) {
    let cookie = match req.cookie("Cookie") {
        None => {
            res.status_code(StatusCode::IM_USED);
            res.render("{\"Error\":\"Cookie not found\"}");
            ctrl.skip_rest();
            return;
        }
        Some(odk) => odk,
    };
    let local = depot.obtain::<LocalCookie>().unwrap();
    let value = local.is_expert(cookie.clone()).await;
    if !value  {
        res.status_code(StatusCode::IM_USED);
        res.render("{\"Error\":\"User not admin\"}");
        ctrl.skip_rest();
    } else {
        let mut nck = req.cookie("Cookie").unwrap().clone();
        nck.set_expires(LocalCookie::future_60min());
        nck.set_path("/");
        res.add_cookie(nck.clone());
    }
}
/// 中间件
/// 检查是否是登录
#[handler]
pub async fn check_login(
    req: &mut Request,
    depot: &mut Depot,
    res: &mut Response,
    ctrl: &mut FlowCtrl,
) {
    let cookie = match req.cookie("Cookie") {
        None => {
            res.status_code(StatusCode::IM_USED);
            res.render("{\"Error\":\"Cookie not found\"}");
            ctrl.skip_rest();
            return;
        }
        Some(odk) => odk,
    };
    let local = depot.obtain::<LocalCookie>().unwrap();
    let value = local.is_login(cookie.clone()).await;
    if !value  {
        res.status_code(StatusCode::IM_USED);
        res.render("{\"Error\":\"User not admin\"}");
        ctrl.skip_rest();
    } else {
        let mut nck = req.cookie("Cookie").unwrap().clone();
        nck.set_expires(LocalCookie::future_60min());
        nck.set_path("/");
        res.add_cookie(nck.clone());
    }
}
